<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\KolManage;

use App\Models\KolManage\Kol;
use Illuminate\Support\Facades\DB;

class KolAjax{

    /**
     * 停用
     * @param $ids
     * @return \Illuminate\Http\JsonResponse
     */
    public function disableItem($ids){
        try{
            $collection = Kol::query()->findMany($ids);
            $count = 0;
            foreach ($collection as $model) {
                DB::beginTransaction();
                if($model->status == Kol::STATUS_NO){
                    DB::rollBack();
                    continue;
                }
                $model->status = Kol::STATUS_NO;
                if(!$model->save()){
                    DB::rollBack();
                }else{
                    $count++;
                    DB::commit();
                }
            }
            return response()->json(['status' => true, 'message' => "成功停用{$count}条记录！"]);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * 启用
     * @param $ids
     * @return \Illuminate\Http\JsonResponse
     */
    public function enableItem($ids){
        try{
            $collection = Kol::query()->findMany($ids);
            $count = 0;
            foreach ($collection as $model) {
                DB::beginTransaction();
                if($model->status == Kol::STATUS_YES){
                    DB::rollBack();
                    continue;
                }
                $model->status = Kol::STATUS_YES;
                if(!$model->save()){
                    DB::rollBack();
                }else{
                    $count++;
                    DB::commit();
                }
            }

            return response()->json(['status' => true, 'message' => "成功启用{$count}条记录！"]);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

}
